package com.zdw.demo.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdw.common.constant.common.CommonConstants;
import com.zdw.demo.entity.WechatOrder;
import com.zdw.demo.mapper.WechatOrderMapper;
import com.zdw.demo.service.WechatOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * Service : 微信支付订单 服务类
 * 用于处理实际的业务逻辑。
 */
@Service
@Slf4j
public class WechatOrderServiceImpl extends ServiceImpl<WechatOrderMapper, WechatOrder> implements WechatOrderService {

    @Autowired
    private WechatOrderMapper orderMapper;


    /**
     * 新增/更新订单信息
     *
     * @param order
     * @return
     */
    public boolean saveOrUpdate(WechatOrder order) {
        LambdaQueryWrapper<WechatOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WechatOrder::getOut_trade_no, order.getOut_trade_no());
        WechatOrder wechatPaymentOrder = orderMapper.selectOne(queryWrapper);
        if (ObjectUtil.isEmpty(wechatPaymentOrder)) {
            return orderMapper.insert(order) > 0;
        } else {
            return orderMapper.updateInfo(order) > 0;
        }
    }

    /**
     * 根据内部订单号 更新订单信息
     *
     * @param order
     * @return
     */
    public boolean updateByOutTradeNo(WechatOrder order) {
        return orderMapper.updateByOutTradeNo(order) > 0;
    }

    /**
     * 批量更新订单信息
     *
     * @param orderList
     * @return
     */
    @Transactional
    public boolean batchUpdateByOutTradeNo(List<WechatOrder> orderList) {
        return orderMapper.batchUpdateByOutTradeNo(orderList) > 0;

    }

    /**
     * 根据内部订单号 关闭订单
     *
     * @param outTradeNo
     * @return
     */
    @Transactional
    public boolean closeByOutTradeNo(String outTradeNo) {
        return orderMapper.closeByOutTradeNo(outTradeNo) > 0;
    }

    /**
     * 根据微信订单号 查询订单信息
     *
     * @param transactionId
     * @return
     */
    public WechatOrder selectByTransactionId(String transactionId) {
        LambdaQueryWrapper<WechatOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WechatOrder::getTransaction_id, transactionId);
        return orderMapper.selectOne(queryWrapper);
    }

    /**
     * 根据订单号 查询订单信息
     *
     * @param outTradeNo
     * @return
     */
    public WechatOrder selectByOutTradeNo(String outTradeNo) {
        LambdaQueryWrapper<WechatOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WechatOrder::getOut_trade_no, outTradeNo);
        return orderMapper.selectOne(queryWrapper);
    }

    /**
     * 查询所有待支付的订单
     *
     * @return
     */
    public List<WechatOrder> selectNotPayOrder() {
        LambdaQueryWrapper<WechatOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WechatOrder::getTrade_state, CommonConstants.WECHAT_TRADE_STATE_NOTPAY);
        return orderMapper.selectList(queryWrapper);
    }


}
